package cn.wzptsoft.mall.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import cn.wzptsoft.mall.mapper.SkuMapper;
import cn.wzptsoft.mall.mapper.SpuImageItemMapper;
import cn.wzptsoft.mall.mapper.SpuMapper;
import cn.wzptsoft.mall.mapper.ThemeMapper;
import cn.wzptsoft.mall.pojo.Sku;
import cn.wzptsoft.mall.pojo.Spu;
import cn.wzptsoft.mall.pojo.SpuImageItem;
import cn.wzptsoft.mall.pojo.dto.PageDto;
import cn.wzptsoft.mall.pojo.vo.*;
import cn.wzptsoft.mall.service.ISpuService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class ISpuServiceImpl extends ServiceImpl<SpuMapper, Spu> implements ISpuService {

    @Autowired
    private SkuMapper skuMapper;
    @Autowired
    private SpuImageItemMapper spuImageItemMapper;

    @Autowired
    private ThemeMapper themeMapper;

    @Override
    public PageVo<SpuInfoVo> pageSpu(PageDto pageDto) {
        Page<Spu> page = Page.of(pageDto.getStart(), pageDto.getCount());
        OrderItem order = new OrderItem();
        order.setColumn("id");
        page.addOrder(order);
        Page<Spu> spuPage = page(page);
        PageVo<SpuInfoVo> pageVo = PageVo.<SpuInfoVo>builder()
                .total(spuPage.getTotal())
                .pages(spuPage.getPages())
                .list(spuPage.getRecords().stream().map(SpuInfoVo::new).toList())
                .build();

        return pageVo;
    }

    @Override
    public SpuDetailVo getdetailSpu(Integer id) {
        Spu spu = getById(id);
        LambdaQueryWrapper<Sku> skuwrapper = new LambdaQueryWrapper<>();
        skuwrapper.eq(Sku::getSpuId, id);
        List<Sku> skus = skuMapper.selectList(skuwrapper);

        LambdaQueryWrapper<SpuImageItem> siiWrapper = new LambdaQueryWrapper<>();
        siiWrapper.eq(SpuImageItem::getSpuId, id);
        List<SpuImageItem> spuImageItems = spuImageItemMapper.selectList(siiWrapper);


        SpuDetailVo spuDetailVo = BeanUtil.copyProperties(spu, SpuDetailVo.class);
        spuDetailVo.setSkus(skus.stream().map(s -> BeanUtil.copyProperties(s, SkuVo.class)).toList());
        spuDetailVo.setSpuImageItems(spuImageItems.stream().map(s -> BeanUtil.copyProperties(s, SpuImageItemVo.class)).toList());

        return spuDetailVo;
    }

    @Override
    public List<SpuInfoVo> listSpuByThemeName(String title) {
        QueryWrapper<Spu> wrapper=new QueryWrapper<>();
        wrapper.eq("t.title",title);
        List<SpuInfoVo> vo= baseMapper.listSpuByThemeName(wrapper);
        return vo;
    }

    @Override
    public List<SpuInfoVo> getAll() {
        List<Spu> list = list();

        List<SpuInfoVo> spuInfoVos = list.stream().map(i->{
            LambdaQueryWrapper<Sku> skuwrapper = new LambdaQueryWrapper<>();
            skuwrapper.eq(Sku::getSpuId, i.getId());
            List<Sku> skus = skuMapper.selectList(skuwrapper);
//            SpuInfoVo vo=new SpuInfoVo(i);
            SpuInfoVo vo = BeanUtil.copyProperties(i, SpuInfoVo.class);
            vo.setSkus(skus.stream().map(j->BeanUtil.copyProperties(j,SkuVo.class)).toList());
            return vo;
        }).toList();
        return spuInfoVos;
    }
}
